<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License edit by kim
 */

class b2c_member_company{
  //保存修改编辑后的付款公司
	public function purchase_save_company($arr_post=array(),$member_id,&$msg=''){
        //检查必填信息是否存在
        if($arr_post['c_type']=='3'){
            if( !$this->check_com_post1($arr_post,$member_id,$msg) ) return false;
        }else{
            if( !$this->check_com_post($arr_post,$member_id,$msg) ) return false;
        }
        
		$arr_post = utils::_filter_input($arr_post);
		foreach($arr_post as $k=>$v){
			$arr_post[$k] = strip_tags($v);
		}

        $member_com_model = app::get('b2c')->model('member_company');
        /*需要存储的数据*/
        if($arr_post['c_type']=='3'){
            $save_data = array(
                'inn_p' => $arr_post['inn_p'],
                'tel' => $arr_post['tel'],
                'c_type' => $arr_post['c_type'],
                'name' => $arr_post['name'],
                'member_id' => $member_id,
                'phone_setting' => $arr_post['phone_setting'],
            );  
        }else{
            $save_data = array(
                'member_id' => $member_id,
                'p_area' => $arr_post['p_area'],
                'p_addr' => $arr_post['p_addr'],
                'area' => $arr_post['area'],
                'addr' => $arr_post['addr'],
                'name' => $arr_post['name'],
                'bank_no' => $arr_post['bank_no'],
                'open_account' => $arr_post['open_account'],
                'inn' => $arr_post['inn'],
                'bik' => $arr_post['bik'],
                'kc' => $arr_post['kc'],
                'kpp' => $arr_post['kpp'],
                'ogrn' => $arr_post['ogrn'],
                'okpo' => $arr_post['okpo'],
                'c_type' => $arr_post['c_type'],
            );
        }

//        $save_data = array(
//            'member_id' => $member_id,
//            'p_area' => $arr_post['p_area'],
//            'p_addr' => $arr_post['p_addr'],
//            'area' => $arr_post['area'],
//            'addr' => $arr_post['addr'],
//            'name' => $arr_post['name'],
//            'bank_no' => $arr_post['bank_no'],
//            'open_account' => $arr_post['open_account'],
//            'inn' => $arr_post['inn'],
//            'bik' => $arr_post['bik'],
//            'kc' => $arr_post['kc'],
//            'kpp' => $arr_post['kpp'],
//            'ogrn' => $arr_post['ogrn'],
//            'okpo' => $arr_post['okpo'],
//            'c_type' => $arr_post['c_type'],
//            'inn_p' => $arr_post['inn_p'],    
//            'tel' => $arr_post['tel'],
//            'phone_setting' => $arr_post['phone_setting'],
//        );
        
        $row = $member_com_model->getList('company_id',$save_data);
        if($row && $row[0]['company_id'] != $arr_post['company_id'] ){
            $msg = app::get('b2c')->_('付款人重复');return false;
        }
        if(!$arr_post['company_id']){
            if($member_com_model->count(array('member_id'=>$member_id)) == 6){
                $msg = app::get('b2c')->_('最多只能添加6个付款公司，请先删除一条之后再添加');
                return false;
            }
        }

        /*订单如果是新增收货地址，设置为默认地址
         *会员中心根据提交数据来是否设置为默认地址
         */
        if(!$arr_post['company_id'] && !isset($arr_post['def_company']) ){
            $save_data['def_company'] = 1;
        }else{
            $save_data['def_company']  = $arr_post['def_company'];
            $company_id = $save_data['company_id'] = $arr_post['company_id'];
        }
        //save
        if(!$update_coms_id = $member_com_model->set_default_coms($save_data,$company_id,$member_id,$msg)){
            return false;
        }
        $save_data['company_id'] = $update_coms_id;
        return $save_data;
	}

    //验证收货地址必填参数是否存在
    function check_com_post($arr_post,$member_id,&$msg=''){
        if(empty($arr_post) || empty($member_id)){
            $msg = app::get('b2c')->_('缺少参数');
			return false;
        }

        if(!$arr_post['name']){
            $msg = app::get('b2c')->_('公司名称未填写');
			return false;
        }

		/*

        if(!$arr_post['bank_no']){
            $msg = app::get('b2c')->_('银行账户未填写');
			return false;
        }

        if(!$arr_post['open_account']){
            $msg = app::get('b2c')->_('开户行未填写');
			return false;
        }*/

        if($arr_post['company_id'] && !app::get('b2c')->model('member_company')->getList('company_id',array('company_id'=>$arr_post['company_id'],'member_id'=>$member_id))){
            $msg = app::get('b2c')->_('更新的付款公司不存在');
			return false;
        }

        return  true;
    }

    function check_com_post1($arr_post,$member_id,&$msg=''){
        if(empty($arr_post) || empty($member_id)){
            $msg = app::get('b2c')->_('缺少参数');
			return false;
        }
        if(!$arr_post['tel']){
            $msg = app::get('b2c')->_('手机号不能为空');
			return false;
        }
        if($arr_post['company_id'] && !app::get('b2c')->model('member_company')->getList('company_id',array('company_id'=>$arr_post['company_id'],'member_id'=>$member_id))){
            $msg = app::get('b2c')->_('更新的付款公司不存在');
			return false;
        }

        return  true;
    }
}

